/**
 * 阿有 email:37798955@qq.com
 * 2017-11
 */
//fmt='yyyy-MM-dd hh:mm:ss'


//数组去重,仅适用于单个列值得数组
export function arrDistinct(arr) {
	var arr1 = []
	for (var i = 0; i < arr.length; i++) {
		var flag = true
		for (var j = 0; j < arr1.length; j++) {
			if (arr1[j] == arr[i]) {
				break;
				flag = false;
			}
		}
		if (flag)
			arr1.push(arr[i])
	}
	return arr1;
}



//数组编织对象
//实现 对数组的排序,分组,左联接

export var _JO = (function () {
	this.concat = function (a, b) {
		a = a | [];
		for (var i = 0; i < b.length; i++) {
			a.push(b[i]);
		}
		return a;
	}
	//orderby="field1%a,field3,field4%,field2%d" 4种格式 分别表示升序,升序,升序,降序
	//模仿select order by 功能
	this.sort = function (arr, orderby) {
		if (!(arr instanceof Array)) return null;
		var sp = orderby.split(',')
		for (var i = sp.length - 1; i >= 0; i--) {
			var j = sp[i].indexOf('%');
			var field = null;
			var hz = null
			if (j > 0) {
				field = sp[i].slice(0, j);
				hz = sp[i].slice(j);
			} else {
				field = sp[i].slice(0)
			}
			if (j > 0 && (hz == '%d' || hz == '%D')) {
				arr = arr.sort(sortcompare(field, 'd')) //降序排列			
			} else {
				arr = arr.sort(sortcompare(field)) //升序排列
			}
		}
		return arr;
	}
	this.move = function (arr1, i, n) {
		var a = arr1[i]
		var j = i + n
		j = j < 0 ? 0 : j;
		if (arr1[i]) {
			arr1.splice(i, 1);
		}
		arr1.splice(j, 0, a);
		return arr1;
	}
	//私有函数 type==null||type==''||type=='a' 
	//升序排列 否则 =='d'倒叙排列
	function sortcompare(prop, type) {
		return function (obj1, obj2) {
			var val1 = obj1[prop];
			var val2 = obj2[prop];
			if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
				val1 = Number(val1);
				val2 = Number(val2);
			}
			var r = 0;
			if (val1 < val2) {
				r = -1;
			} else if (val1 > val2) {
				r = 1;
			}
			if (type == 'd') {
				return 0 - r;
			} else
				return r;
		}
	}

	//内联接 arr1 主表 ,arr2 子表, joins 连接字段 "字段1=字段1,字段2=字段2"
	//模仿select left join 功能
	this.innerjoin = function (arr1, arr2, joinstr) {
		var a = []
		var b = []
		var joins = joinstr.split(",")
		for (var k = 0; k < joins.length; k++) {
			var obj = joins[k].split('=')
			if (obj.length == 2) {
				a.push(obj[0])
				b.push(obj[1])
			}
		}
		var arr = [];
		for (var i = 0; i < arr1.length; i++) {
			var obj1 = arr1[i]
			for (var j = 0; j < arr2.length; j++) {
				var obj2 = arr2[j]
				var flag = 1;
				for (var k = 0; k < a.length; k++) {
					if (obj1[a[k]] != obj2[b[k]]) {
						flag = 0;
						break;
					}
				}
				if (flag == 1) {
					var obj = mergeObj(obj1, obj2)
					arr.push(obj)
				}
			}
		}
		return arr;
	}
	//如果两张表的字段相同必须提前select
	//左联接 arr1 主表 ,arr2 子表, joins 连接字段 "字段1=字段1,字段2=字段2"
	this.leftjoin = function (arr1, arr2, joinstr) {
		var a = []
		var b = []
		var joins = joinstr.split(",")
		//拆解 on 字段1=字段2,字段1=字段2,字段1=字段2
		for (var k = 0; k < joins.length; k++) {
			var obj = joins[k].split('=')
			if (obj.length == 2) {
				a.push(obj[0])
				b.push(obj[1])
			}
		}
		var arr = [];
		for (var i = 0; i < arr1.length; i++) {
			var obj1 = arr1[i]
			var neq = true; //假设不会匹配到,如果匹配到 =false
			for (var j = 0; j < arr2.length; j++) {
				var obj2 = arr2[j]
				var flag = 1;
				for (var k = 0; k < a.length; k++) { //所有条件都满足
					if (obj1[a[k]] != obj2[b[k]]) {
						flag = 0; //有一个条件不满足
						break;
					}
				}
				if (flag == 1) {
					var obj = mergeObj(obj1, obj2)
					arr.push(obj)
					neq = false
				}

			}
			if (neq)
				arr.push(obj1);
		}
		return arr;
	}
	//合并o1和o2 生成新的对象,可以只选择抽取o1和o2中的部分字段
	function mergeObj(o1, o2, o1fields, o2fields) {
		var fis = null;
		if (o1fields)
			fis = o1fields.split(',')
		var n1 = clone(o1, fis);
		if (o2fields == null) {
			for (var key in o2) {
				n1[key] = o2[key]
			}
		} else {
			var fis2 = o1fields.split(',')
			for (var key in fis2) {
				n1[key] = o2[key]
			}
		}
		return n1;
	}
	//arr 表示 待分组的数据
	//fields 表示去重的字段
	//根据fieldstr 对数组中的对象进行去重
	//模仿select distinct 功能
	this.distinct = function (arr, fieldstr) {
		var arr1 = []
		var fields = fieldstr.split(',')
		for (var i = 0; i < arr.length; i++) {
			var obj1 = arr[i]
			var flag1 = 1
			//console.log( arr1.length)
			for (var j = 0; j < arr1.length; j++) {
				var obj2 = arr1[j]
				if (equalprop(obj1, obj2, fields)) { //如果属性值相同
					flag1 = 0
					break;
				} else {
					continue;
				}
			}
			if (flag1 == 1)
				arr1.push(section(obj1, fields))
		}
		return arr1;
	}
	//根据属性截取对象的部分属性 生成新的对象
	function section(object, cols) {
		var n1 = {}
		var props = null
		if (cols == null || cols == '') return object;
		if (cols instanceof String)
			props = cols.split(',')
		else
			props = cols
		for (var i = 0; i < props.length; i++) {
			n1[props[i]] = object[props[i]]
		}
		return n1

	}
	//groupcols= group by 后面的字段
	//countcols="field1","field2"..，待统计的之短
	//模仿group by count 功能
	this.groupby = function (tarr, groupcols, countcols) {
		var arr = this.sort(tarr, groupcols) //先排序
		//		for(var i in arr) {
		//			console.log(JSON.stringify(arr[i]))
		//		}
		var arr1 = [] //
		var fields = groupcols.split(',')
		var cols = countcols.split(',') //统计字段
		var ob1 = null;
		for (var i = 0; i < arr.length; i++) {
			var obj1 = arr[i]
			var flag1 = 1 //分组标志
			//console.log( arr1.length)
			for (var j = 0; j < arr1.length; j++) {
				var obj2 = arr1[j]
				if (equalprop(obj1, obj2, fields)) {
					flag1 = 0 //维值相等 继续累加
					for (var k = 0; k < cols.length; k++) {
						var col = cols[k]
						ob1[col]["count"]++;
						ob1[col]["lst"] = obj1[col];
						ob1[col]["sum"] += obj1[col];
						if (ob1[col]["min"] > obj1[col])
							ob1[col]["min"] = obj1[col]
						else if (ob1[col]["max"] < obj1[col])
							ob1[col]["max"] = obj1[col];
					}
				}
			}
			if (flag1 == 1) { //维值不等 新建一条
				ob1 = section(obj1, fields) //截取部分字段
				//对每个字段进行基本统计
				for (var k = 0; k < cols.length; k++) {
					var col = cols[k]
					ob1[col] = {}
					ob1[col]["count"] = 1;
					ob1[col]["sum"] = obj1[col];
					ob1[col]["min"] = obj1[col];
					ob1[col]["max"] = obj1[col];
					ob1[col]["fst"] = obj1[col];
					ob1[col]["lst"] = obj1[col];
				}
				arr1.push(ob1)
			}
		}
		return arr1;
	}

	//对比两个对象的特定属性 判断是否相等
	function equalprop(obj1, obj2, fields) {
		//console.log(fields)
		var flag = 1
		for (var i = 0; i < fields.length; i++) {
			var field = fields[i]
			if (obj1[field] == obj2[field]) {
				continue;
			} else {
				flag = 0;
				break;
			}
		}
		if (flag == 0) {
			return false;
		} else
			return true;
	}
	//fields="field1 field11,field2 field12,field3 field13" 把field1转为field11属性 ,其他一样
	//实现select as 功能.并截取指定列名,无指定不显示
	this.select = function (arr, fields) {
		var arr1 = [];
		for (var i = 0; i < arr.length; i++) {
			var n1 = this.selectcols(arr[i], fields);
			arr1.push(n1)
		}
		//console.log("n1"+JSON.stringify(arr1));
		return arr1;
	}
	this.selectcols = function (rc, select) {
		//console.log("selectcols::"+select);
		if (select == null || select == '') return rc;
		var keys = select.split(',')
		var ar = [] //原字段数组
		var br = [] //新列名数组
		//分解老新列名
		for (var i = 0; i < keys.length; i++) {
			var astr = null;
			if (keys[i].indexOf(':') > -1)
				astr = keys[i].split(':');
			else if (keys[i].indexOf(' ') > -1)
				astr = keys[i].split(' ');
			else if (keys[i].indexOf(' as ') > -1)
				astr = keys[i].split(' as ');
			else
				astr = keys[i];

			if (astr == null) continue;
			if (_IS.isArray(astr)) {
				if (astr[0] in rc) {
					ar.push(astr[0]);
					br.push(astr[1]);
				}
			} else {
				if (astr in rc) {
					ar.push(astr);
					br.push(astr);
				}
			}

		}
		if (ar.length == 0) return;
		var n1 = {} //目标对象
		for (var j = 0; j < ar.length; j++) {
			n1[br[j]] = rc[ar[j]]
		}
		//console.log("n1"+JSON.stringify(n1));
		return n1;
	}

	this.getColList = function (arr, column, filter) {
		var arr1 = [] //目标数组
		if (arr.length == 0) return;
		if (column in arr[0]) {
			for (var i = 0; i < arr.length; i++) {
				var em = arr[i][column];
				if (filter) {
					em = filter(em)
				}
				arr1.push(em);
			}
		}
		return arr1;
	}

});
